package dao

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	cfg "learn-api/common/config"

	"learn-api/models"
)

// 初始化数据库
func init() {

	// 从配置中心取参数
	var runmode = cfg.GetString("runmode")
	// Logger
	//var logLevel = cfg.GetString("log_level")
	// mysql
	var mysqlUrl = cfg.GetString("mysql_url")
	var mysqlUserName = cfg.GetString("mysql_username")
	var mysqlPassword = cfg.GetString("mysql_password")
	var mysqlMaxIdle, _ = cfg.GetInt("mysql_max_idle")
	var mysqlMaxConn, _ = cfg.GetInt("mysql_max_conn")
	if runmode == "dev" {
		orm.Debug = true
	}

	// set default database
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", fmt.Sprintf("%s:%s@%s", mysqlUserName, mysqlPassword, mysqlUrl), mysqlMaxIdle, mysqlMaxConn)

	orm.RegisterModel(new(models.UserDataModel))
}
